﻿Imports System.ComponentModel

Public Class AcctFindPopWin

    Private m_AcctList As List(Of ClsTAccount) = New List(Of ClsTAccount)()
    Private m_FindAcctRowIndex As Integer '当前列表选中项索引
    Private m_FindAcctText As String  '要搜索的字串
    Private m_FindAcctResult As Boolean '搜索结果
    Public Delegate Sub FindCallBack(ByVal sender As Object, ByVal e As FindAcctEventArg)
    Private m_AcctFindResultCallBack As FindCallBack
    'Private m_AcctFindList As List(Of ClsTAccount) = New List(Of ClsTAccount)()

    Public Sub New()

        ' 此调用是设计器所必需的。
        InitializeComponent()

        ' 在 InitializeComponent() 调用之后添加任何初始化。

    End Sub

    Public Property AcctList As List(Of ClsTAccount)
        Get
            Return m_AcctList
        End Get
        Set(value As List(Of ClsTAccount))
            m_AcctList = value
        End Set
    End Property

    Public Property FindAcctRowIndex As Integer
        Get
            Return m_FindAcctRowIndex
        End Get
        Set(value As Integer)
            m_FindAcctRowIndex = value
        End Set
    End Property

    Public Property FindOk As Boolean
        Get
            Return m_FindAcctResult
        End Get
        Set(value As Boolean)
            m_FindAcctResult = value
        End Set
    End Property

    Public Property AcctFindResultCallBack As FindCallBack
        Get
            Return m_AcctFindResultCallBack
        End Get
        Set(value As FindCallBack)
            m_AcctFindResultCallBack = value
        End Set
    End Property


    Private Sub lstAcct_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstAcct.SelectedIndexChanged
        If lstAcct.SelectedIndices.Count > 0 Then
            FindAcctRowIndex = lstAcct.SelectedIndices.Item(0)
        Else
            FindAcctRowIndex = -1
        End If

    End Sub

    Private Sub txtInput_TextChanged(sender As Object, e As EventArgs) Handles txtInput.TextChanged
        m_FindAcctText = txtInput.Text.Trim()
        '输入内容就开始搜索，清空内容就清空列表
        SearchFindAcct()
    End Sub

    Private Sub txtInput_KeyDown(sender As Object, e As KeyEventArgs) Handles txtInput.KeyDown
        '处理回车
        Select Case e.KeyCode
            Case Keys.Enter '回车
                '跳到列表
                lstAcct.Focus()
                e.Handled = True
            Case Keys.Down '下光标
                lstAcct.Focus()
                e.Handled = True
        End Select
    End Sub

    Private Sub SearchFindAcct() 'As Boolean

        Dim fOne As Boolean

        '每次搜索都清空原有列表内容
        lstAcct.Items.Clear()

        '清除上次搜索结果
        FindOk = False

        If String.IsNullOrEmpty(m_FindAcctText) Then
            Return
        End If

        For Each tmpA As ClsTAccount In AcctList
            fOne = False
            If tmpA.AccountCode.IndexOf(m_FindAcctText) <> -1 Then

                fOne = True

                '没有在编码中找到，则在名称中查找 
            ElseIf tmpA.AccountName.IndexOf(m_FindAcctText) <> -1 Then
                fOne = True

            End If

            If fOne Then
                Dim tmpItem As ListViewItem = New ListViewItem(tmpA.AccountCode)

                tmpItem.SubItems.Add(tmpA.PrevLevelCode)
                tmpItem.SubItems.Add(tmpA.AccountName)
                lstAcct.Items.Add(tmpItem)
            End If

        Next

        If lstAcct.Items.Count > 0 Then
            FindOk = True
            lstAcct.Items.Item(0).Selected = True
        End If


    End Sub


    Private Sub AcctFindPopWin_Load(sender As Object, e As EventArgs) Handles Me.Load
        '设置列表头
        Dim cols(2) As ColumnHeader
        '初始化列表标题
        Dim header1 As ColumnHeader = New ColumnHeader()
        header1.Text = "科目编号"
        header1.Width = 80
        header1.TextAlign = HorizontalAlignment.Left
        header1.Name = "hAcctCode"
        cols(0) = header1
        Dim header2 As ColumnHeader = New ColumnHeader()
        header2.Text = "上级科目"
        header2.Width = 60
        header2.TextAlign = HorizontalAlignment.Left
        header2.Name = "hPrevCode"
        cols(1) = header2
        Dim header3 As ColumnHeader = New ColumnHeader()
        header3.Text = "科目名称"
        header3.Width = 100
        header3.TextAlign = HorizontalAlignment.Left
        header3.Name = "hAcctName"
        cols(2) = header3

        With lstAcct
            .FullRowSelect = True '行选择模式
            .MultiSelect = False '不允许 多行选择
            .View = View.Details '视图选项：详细模式
            '.Width = Me.Width  '宽度占满窗体
            .Scrollable = True
            .HeaderStyle = ColumnHeaderStyle.Nonclickable '标题不可点击
            .Columns.AddRange(cols)
        End With

        'LoadAcctToListView()
    End Sub

    Private Sub lstAcct_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles lstAcct.MouseDoubleClick
        If m_FindAcctRowIndex <> -1 Then
            Me.Close()
        End If
    End Sub

    Private Sub AcctFindPopWin_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
        SetCallBack()
    End Sub

    Private Sub SetCallBack()
        If AcctFindResultCallBack IsNot Nothing Then
            Dim e1 As FindAcctEventArg = New FindAcctEventArg()
            If FindAcctRowIndex <> -1 Then
                e1.AcctCode = lstAcct.Items(FindAcctRowIndex).SubItems(0).Text
                e1.AcctName = lstAcct.Items(FindAcctRowIndex).SubItems(2).Text
                e1.FindOk = True
            Else
                e1.AcctCode = ""
                e1.AcctName = ""
                e1.FindOk = False
            End If
            e1.Msg = "SearchAcctResult"

            AcctFindResultCallBack.Invoke(Me, e1)
        End If
    End Sub

    Private Sub lstAcct_KeyDown(sender As Object, e As KeyEventArgs) Handles lstAcct.KeyDown
        If e.KeyCode = Keys.Enter Then
            Debug.Print("lstAcct 回车")
            If m_FindAcctRowIndex <> -1 Then
                Me.Close()
            End If

        End If
    End Sub

End Class

Public Class FindAcctEventArg
    Inherits EventArgs

    Private m_Msg As String
    Private m_FindOk As Boolean
    Private m_AcctCode As String
    Private m_AcctName As String

    Public Property Msg As String
        Get
            Return m_Msg
        End Get
        Set(value As String)
            m_Msg = value
        End Set
    End Property

    Public Property FindOk As Boolean
        Get
            Return m_FindOk
        End Get
        Set(value As Boolean)
            m_FindOk = value
        End Set
    End Property

    Public Property AcctCode As String
        Get
            Return m_AcctCode
        End Get
        Set(value As String)
            m_AcctCode = value
        End Set
    End Property

    Public Property AcctName As String
        Get
            Return m_AcctName
        End Get
        Set(value As String)
            m_AcctName = value
        End Set
    End Property
End Class